package com.qwert.shortlink.project.toolkit;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.Optional;

import static com.qwert.shortlink.project.common.constant.ShortLinkConstant.*;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/7/6 15:03
 **/
public class LinkUtil {
    /**
     *  创建时设置短链接有效期
     */
    public static long getLinkCacheValidTime(Date validDate) {
        return Optional.ofNullable(validDate)
                .map(each -> {
                    long differenceMs = DateUtil.between(new Date(), each, DateUnit.MS);
                    return Math.min(differenceMs, FIVE_MINUTES);
                })
                .orElse(FIVE_MINUTES);
    }

    /**
     * 给指定key设置有效期
     */
    public static boolean setKeyValidTime(){
        return true;
    }
    /**
     * 给指定key重置有效期，指定时间
     */
    public static boolean expireKeyValid(String key, long time){
        return true;
    }
    public static boolean expireKeyValid(String key){
        return expireKeyValid(key, FIVE_MINUTES);
    }
}
